package com.wantyx.creation.builder;

import com.wantyx.bean.car.Car;
import com.wantyx.bean.car.ZeekeCar;

/**
 * 作者 : wantyx
 * 创建时间 : 2023/11/3
 * 实现功能 :
 *    建造器模式，能够将对象初始化的操作放到统一的位置进行处理。相比于用构造器的方式：避免在构造方法中包含大量的逻辑；
 *    另外像本例子中的音响选购，可能还涉及金钱的操作，能够通过此方式进行选配逻辑的清晰处理。
 */
public class ZeekeCarBuilder implements CarBuilder{
    private ZeekeCar zeekeCar = new ZeekeCar();

    public ZeekeCarBuilder(){
        this.zeekeCar.setName("极氪001");
        this.zeekeCar.setBrand("zeeke");
        this.zeekeCar.setSound(ZeekeCar.ZKSound.ORIGINAL.name());
        this.zeekeCar.setPrice(30);
    }

    @Override
    public CarBuilder sound(String sound) {
        String previousName = this.zeekeCar.getSound();
        ZeekeCar.ZKSound previous = ZeekeCar.ZKSound.valueOf(previousName);
        ZeekeCar.ZKSound current = ZeekeCar.ZKSound.valueOf(sound);
        float price = this.zeekeCar.getPrice();
        float previousPrice = previous.getPrice();
        float currentPrice = current.getPrice();
        price = price - previousPrice + currentPrice;
        this.zeekeCar.setPrice(price);
        this.zeekeCar.setSound(sound);
        return this;
    }

    @Override
    public CarBuilder hub(String hub) {
        return this;
    }

    @Override
    public CarBuilder machine(String machine) {
        return this;
    }

    @Override
    public Car build() {
        return this.zeekeCar;
    }
}
